package cn.edu.jsu.nrs.action;

import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.jsu.nrs.model.News;
import cn.edu.jsu.nrs.model.NewsType;
import cn.edu.jsu.nrs.service.NewsService;
import cn.edu.jsu.nrs.service.NewsTypesService;
import cn.edu.jsu.nrs.util.ConvertArray;

import com.opensymphony.xwork2.ActionSupport;

@Component(value = "newsAction")
@Scope("prototype")
public class NewsAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private List<News> topNews;// 最新新闻
	public List<News> hotNews;// 热点新闻
	private List<News> armNews;// 军事新闻
	private List<News> domesticNews;// 国内新闻
	private List<News> internationalNews;// 国际新闻
	private List<News> socialNews;// 社会新闻
	private List<News> governmentNews;// 政务新闻
	private List<News> digitalNews;// 数码新闻
	private List<News> publicWelfareNews;// 公益新闻
	private List<NewsType> newsTypesList;// 新闻类别列表
	private List<News> newsList;// 新闻列表
	private NewsService newsService;
	private ConvertArray convertArray;
	private NewsTypesService newsTypesService;
	private News news;// 新闻
	private int result;
	private int id;// 新闻ID
	private int typeId;// 新闻类别ID
	private int pageIndex;// 页的索引
	private int pageSize;// 页的大小
	
	private int rows;
	private int page;
	private int totalCount;// 新闻的总记录数
	private int totalPage;// 总页数
	private String sord;// 排序方式
	private String sidx;// 排序字段
	private String keyword;
	private String ids;//要删除的ID

	public void setIds(String ids) {
		this.ids = ids;
	}

	/**
	 * 查询热点新闻
	 * 
	 * @return
	 */
	public String hotNewsList() {
		hotNews = newsService.getHotNews(0, 12);// 热点新闻
		return "hotNewsList";
	}

	/**
	 * 查询最新新闻
	 * 
	 * @return
	 */
	public String topNewsList() {
		topNews = newsService.getTopNews(0, 12);// 最新新闻
		return "topNewsList";
	}

	/**
	 * 查询军事新闻
	 * 
	 * @return
	 */
	public String armNewsList() {
		// armNews=newsService.pagingByTypeId(7, 0, 12);//军事新闻
		armNews = newsService.getNewsListByTypeIdAndCount(7, 12);
		return "armNewsList";
	}

	/**
	 * 查询政务新闻
	 * 
	 * @return
	 */
	public String governmentNewsList() {
		// governmentNews=newsService.pagingByTypeId(9, 0, 8);//政务新闻
		governmentNews = newsService.getNewsListByTypeIdAndCount(9, 8);
		return "governmentNewsList";
	}

	/**
	 * 查询国内新闻
	 * 
	 * @return
	 */
	public String domesticNewsList() {
		// domesticNews=newsService.pagingByTypeId(1, 0, 8);//国内新闻
		domesticNews = newsService.getNewsListByTypeIdAndCount(1, 8);
		return "domesticNewsList";
	}

	/**
	 * 查询国际新闻
	 * 
	 * @return
	 */
	public String internationalNewsList() {
		// internationalNews=newsService.pagingByTypeId(2, 0, 8);//国际新闻
		internationalNews = newsService.getNewsListByTypeIdAndCount(2, 8);
		return "internationalNewsList";
	}

	/**
	 * 查询社会新闻
	 * 
	 * @return
	 */
	public String socialNewsList() {
		// socialNews=newsService.pagingByTypeId(3, 0, 8);//社会新闻
		socialNews = newsService.getNewsListByTypeIdAndCount(3, 8);
		return "socialNewsList";
	}

	/**
	 * 查询数码新闻
	 * 
	 * @return
	 */
	public String digitalNewsList() {
		// digitalNews=newsService.pagingByTypeId(6, 0, 8);//数码新闻
		digitalNews = newsService.getNewsListByTypeIdAndCount(6, 8);
		return "digitalNewsList";
	}

	/**
	 * 查询公益新闻
	 * 
	 * @return
	 */
	public String publicWelfareNewsList() {
		// publicWelfareNews=newsService.pagingByTypeId(10, 0, 8);//公益新闻
		publicWelfareNews = newsService.getNewsListByTypeIdAndCount(10, 8);
		return "publicWelfareNewsList";
	}

	/**
	 * 新闻详细
	 * 
	 * @return
	 */
	public String detail() {
		// id不存在
		if (id == 0) {
			return "error";
		}
		news = newsService.findNewsById(id); // 根据新闻ID找到相应的新闻
		int typeId = news.getTbNewsType().getNewsTypeId();// 得到新闻类别Id
		newsTypesList = newsTypesService.findNewsTypesById(typeId);// 根据新闻类别找出所有类别
		Collections.reverse(newsTypesList);// 将新闻类别逆序
		if (news != null) {
			// 添加点击数
			int clickCount = news.getClickCount() + 1;
			news.setClickCount(clickCount);
			newsService.updateNews(news);
			// 获得跟帖数
			// comments=commentService.getCommentList(id);
		}
		return "detail";// 返回到详细页面
	}
	
	
	/**
	 * 根据新闻类别查询新闻列表相关信息
	 * 
	 * @return
	 */
	public String newsList() {
		if (typeId == 0) {// 返回首页
			return "listFail";// 返回首页
		} else {
			totalCount = newsService.getNewsCounts(typeId);
			newsTypesList = newsTypesService.findNewsTypesById(typeId);// 根据新闻类别找出所有类别
			Collections.reverse(newsTypesList);// 将新闻类别逆序
			return "newsList";// 返回列表
		}
	}

	/**
	 * 查询新闻列表
	 * 
	 * @return
	 */
	public String newsPaging() {
		/*
		 * if (typeId == 0) {// 查询所有新闻列表 newsList = newsService.paging(pageIndex
		 * * pageSize, pageSize, "publishTime");
		 * 
		 * } else {
		 */// 根据新闻类别id查询新闻列表
		newsList = newsService.pagingByTypeId(typeId, pageIndex * pageSize,
				pageSize);
		// }
		return "pageList";
	}

	/**
	 * 查询用户列表
	 * 
	 * @return
	 */
	public String searchNewsList() {
		System.out.println(page+" page"+rows+typeId);
		newsList = newsService.getNewsListBySearch((page - 1) * rows,rows, keyword,typeId,sord, sidx);
		System.out.println("newsList.size()"+newsList.size());
		totalCount = newsService.getNewsCounts(keyword,typeId);
		if (rows!=0 && newsList.size()!=0) {
			totalPage = totalCount/rows + 1;
		}
		return "newsList";
	}

	public String newsInfor(){
		news=newsService.findNewsById(id);
		if (news!=null) {
			return "detail";
		}else {
			return "detail";
		}
	}
	
	/***
	 * 添加新闻
	 */
	public String addNews(){	
		System.out.println(news.getContent()+news.getTbNewsType().getNewsTypeId());
		if (newsService.addNews(news)) {
			result=1;
		}else {
			result=0;
		}
		
		return "addNews";
	}
	
	/**
	 * 删除新闻
	 * @return
	 */
	public String deleteNews() {
		String[] deleteIds=convertArray.convertIdsToArray(ids);
		for (int i = 0; i < deleteIds.length; i++) {//循环取出要删除的用户id
			int id=Integer.parseInt(deleteIds[i].trim());
			News news = newsService.findNewsById(id);// 根据用户id获得用户信息
			if (news != null) {
				newsService.deleteNews(news);// 删除用户
				result=1;
			} else {
				result=0;
				continue;
				
			}
		}
		if (result==1) {
			return "deleteNews";
		}else {
			return "deleteFail";// 删除失败
		}
		
	}
	
	
	@JSON
	public News getNews() {
		return news;
	}

	@JSON
	public List<News> getHotNews() {
		return hotNews;
	}

	@JSON
	public List<News> getTopNews() {
		return topNews;
	}

	@JSON
	public List<News> getArmNews() {
		return armNews;
	}

	@JSON
	public List<News> getDomesticNews() {
		return domesticNews;
	}

	@JSON
	public List<News> getInternationalNews() {
		return internationalNews;
	}

	@JSON
	public List<News> getSocialNews() {
		return socialNews;
	}

	@JSON
	public List<News> getGovernmentNews() {
		return governmentNews;
	}

	@JSON
	public List<News> getDigitalNews() {
		return digitalNews;
	}

	@JSON
	public List<News> getPublicWelfareNews() {
		return publicWelfareNews;
	}

	public void setId(int id) {
		this.id = id;
	}

	@JSON
	public List<News> getNewsList() {
		return newsList;
	}

	@JSON
	public List<NewsType> getNewsTypesList() {
		return newsTypesList;
	}

	@JSON
	public int getTotalCount() {
		return totalCount;
	}

	public int getTypeId() {
		return typeId;
	}

	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	@JSON
	public int getResult() {
		return result;
	}
	@JSON
	public int getRows() {
		return rows;
	}
	@JSON
	public int getTotalPage() {
		return totalPage;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public void setNews(News news) {
		this.news = news;
	}
	@Resource
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}
	@Resource
	public void setConvertArray(ConvertArray convertArray) {
		this.convertArray = convertArray;
	}

	@Resource
	public void setNewsTypesService(NewsTypesService newsTypesService) {
		this.newsTypesService = newsTypesService;
	}

}
